package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class SettingsDialog extends Dialog {

	public static Context Settingscontext;
	public static Dialog d;
	RadioGroup rgSettings;
	Button btSelect, btCancel, btDelete;

	public SettingsDialog(Context context) {
		super(context);
		this.Settingscontext = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.settingsdialog);
		WindowManager.LayoutParams wmlp = this.getWindow().getAttributes();
		WindowManager wm = (WindowManager) this.Settingscontext
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		wmlp.gravity = Gravity.CENTER_VERTICAL;
		wmlp.width = display.getWidth();

		d = SettingsDialog.this;
		
		rgSettings = (RadioGroup) findViewById(R.id.rgSettings);
		ArrayList settingslist = new ArrayList();
		if (fileExistance("listofsettings")) {
			try {
				settingslist = (ArrayList) InternalStorage.readObject(
						Settingscontext, "listofsettings");
				RadioButton rb;
				for (int i = 0; i < settingslist.size(); i++) {
					rb = new RadioButton(Settingscontext);
					rb.setText(settingslist.get(i).toString());
					rb.setId(i);
					rgSettings.addView(rb); // the RadioButtons are added to the
											// radioGroup instead of the layout

				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			RadioButton newSettings = new RadioButton(Settingscontext);
			newSettings.setText("No settings found");
			newSettings.setId(0);
			rgSettings.addView(newSettings);
		}

		btSelect = (Button) findViewById(R.id.btSelect);
		btSelect.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated catch block
				if (rgSettings.getCheckedRadioButtonId() != -1) {
					RadioButton rb = (RadioButton) findViewById(rgSettings
							.getCheckedRadioButtonId());
					String key = rb.getText().toString();
					if(key.equals("No settings found")){
						dismiss();
					}
					else{
					try {
						boolean filecheck = fileExistance(key);
						Settings ss1 = (Settings) InternalStorage.readObject(
								Settingscontext, key);
						RotationActivity.settings = ss1;
						Intent gotonextactivity = new Intent(Settingscontext,
								AutoActivity.class);
						/*
						byte[] bytes;
						if (RotationActivity.activeBmp == null) {
							bytes = compressImage(RotationActivity.originalBmp);
						} else {
							bytes = compressImage(RotationActivity.activeBmp);
						}
						*/
						
						try {
							if (RotationActivity.activeBmp == null) {
								InternalStorage.writeBitmap(Settingscontext, "rotatedBmp", RotationActivity.originalBmp);
							}
							else
							{
								InternalStorage.writeBitmap(Settingscontext, "rotatedBmp", RotationActivity.activeBmp);
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						gotonextactivity.putExtra("analysisorigin", RotationActivity.analysisorigin);
						dismiss();
						//gotonextactivity.putExtra("BMP", bytes);
						Settingscontext.startActivity(gotonextactivity);

					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				}
			}
		});

		btCancel = (Button) findViewById(R.id.btCancel);
		btCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		
		btDelete = (Button)findViewById(R.id.btDelete);
		btDelete.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				if (rgSettings.getCheckedRadioButtonId() != -1) {
					RadioButton rb = (RadioButton) findViewById(rgSettings
							.getCheckedRadioButtonId());

					String key = rb.getText().toString();
					if (key != "Create New Marker") {
						DeleteDialog deleteDialog = new DeleteDialog(
								Settingscontext, "SettingsDialog", key);
						deleteDialog.show();
					}
				}
			}
		});

	}

	public boolean fileExistance(String key) {
		File file = Settingscontext.getFileStreamPath(key);
		return file.exists();
	}

	public byte[] compressImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 100, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;

	}
	
	public static void deleteFile(String key) {
		if (key != "Create New Marker") {
			
			ArrayList standardlist;
			try {
				standardlist = (ArrayList) InternalStorage.readObject(
						Settingscontext, "listofsettings");
				for (int i = 0; i < standardlist.size(); i++) {
					if (standardlist.get(i).equals(key)) {
						standardlist.remove(i);
					}
				}
				InternalStorage.deleteObject(Settingscontext, key);
				InternalStorage.writeObject(Settingscontext,
						"listofsettings", standardlist);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			;
		}

		d.dismiss();
		Toast.makeText(Settingscontext, "Setting deleted", Toast.LENGTH_SHORT)
				.show();
	}
}
